//
//  ThirdViewController.m
//  AVFloatView
//
//  Created by hulinhu on 2020/10/22.
//

#import "ThirdViewController.h"
#import <AVKit/AVKit.h>

@interface ThirdViewController ()<AVPlayerViewControllerDelegate>
@property (nonatomic, strong) AVPlayerItem *playerItem;
@property (nonatomic, strong) AVPlayer *player;
@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    //  返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton .frame = CGRectMake(self.view.frame.size.width - 60, 30, 40, 40);
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    
    AVPlayerViewController *avplayerVC = [[AVPlayerViewController alloc] init];
    avplayerVC.view.backgroundColor = [UIColor whiteColor];
    avplayerVC.delegate = self;
    avplayerVC.allowsPictureInPicturePlayback = YES;
    avplayerVC.view.frame = CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height-80);
    [self.view addSubview:avplayerVC.view];
    [self addChildViewController:avplayerVC];
    
    
    NSString *m3u8PlayUrl = @"https://play.mitao.cn/live/wdStream_prod_45151_1838639080.m3u8?txSecret=b671e2f35fbc147f27e9713bbad2aeea&txTime=5F915820";
    NSString *flvPlayUrl = @"https://play.mitao.cn/live/wdStream_prod_45151_1838639080.flv?txSecret=b671e2f35fbc147f27e9713bbad2aeea&txTime=5F915820";
    NSString *mp4Url = @"https://vd2.bdstatic.com/mda-kjkpvdnzxngm6s5s/sc/cae_h264_clips/1603271118/mda-kjkpvdnzxngm6s5s.mp4";
    
    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:m3u8PlayUrl]];
    self.playerItem = playerItem;

    AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
    avplayerVC.player = player;
    [player play];
    self.player = player;
    [playerItem addObserver:self forKeyPath:@"status"options:NSKeyValueObservingOptionNew context:nil];

    
}

- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context{
    if([object isKindOfClass:[AVPlayerItem class]]) {
        if([keyPath isEqualToString:@"status"]) {
            switch(self.playerItem.status) {
                case AVPlayerItemStatusReadyToPlay://推荐将视频播放放这里
                    [self.player play];
                    NSLog(@"AVPlayerItemStatusReadyToPlay");

                    break;
                case AVPlayerItemStatusUnknown:
                    NSLog(@"AVPlayerItemStatusUnknown");
                    break;
                case AVPlayerItemStatusFailed:
                   ;
                    NSLog(@"StatusFailed---- %@", self.playerItem.error.description);
                    NSLog(@"AVPlayerItemStatusFailed");
                    break;
                default:
                    break;
             }
        }
    }
}


-(void)backBtnClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark
#pragma mark - delegate

- (void)playerViewController:(AVPlayerViewController *)playerViewController willBeginFullScreenPresentationWithAnimationCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator  {
    
}

- (void)playerViewController:(AVPlayerViewController *)playerViewController willEndFullScreenPresentationWithAnimationCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    
}

- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"将要开始PictureInPicture的代理方法");
    
}

- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error {
    NSLog(@"启动PictureInPicture失败的代理方法");
}

- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController{
    NSLog(@"将要停止PictureInPicture的代理方法");
}

- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"已经停止PictureInPicture的代理方法");
}

- (BOOL)playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart:(AVPlayerViewController *)playerViewController{
    return YES;
}

- (void)playerViewController:(AVPlayerViewController *)playerViewController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler {
    //此方法执行在pictureInPictureControllerWillStopPictureInPicture代理方法之后，在pictureInPictureControllerDidStopPictureInPicture执行之前。 但是点击“X”移除画中画时，不执行此方法。
    NSLog(@"PictureInPicture停止之前恢复用户界面");
}

-(void)dealloc {
    NSLog(@"dealloc ---- ");
}


@end

